<?php

namespace app\models;

use Yii;
use yii\db\Query;
/**
 * This is the model class for table "gky_qyh_sp_statement".
 *
 * @property integer $id
 * @property integer $approver_id
 * @property integer $proposer_id
 * @property string $responsible
 * @property string $correlation1
 * @property string $correlation2
 * @property string $create_time
 * @property integer $status
 * @property srting $print_code
 * @property string $agree_time
 * @property string $signature
 */
class GkyQyhSpStatement extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'gky_qyh_sp_statement';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['approver_id', 'proposer_id', 'responsible', 'correlation1', 'correlation2', 'create_time', 'status'], 'required'],
            [['approver_id', 'proposer_id', 'status'], 'integer'],
            [['create_time', 'agree_time'], 'safe'],
            [['responsible'], 'string', 'max' => 50],
            [['correlation1', 'correlation2'], 'string', 'max' => 100],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'approver_id' => 'Approval ID',
            'proposer_id' => 'Proposer',
            'responsible' => 'Responsible',
            'correlation1' => 'Correlation1',
            'correlation2' => 'Correlation2',
            'create_time' => 'Create Time',
            'status' => 'Status',
            'agree' => 'Agree',
        ];
    }
     public static function getStatement($printCode) {
        return (new Query())
                        ->select('u.avatar,u.name,l.*,ul.signature')
                        ->from('gky_qyh_sp_statement as l')
                        ->leftJoin('gky_qyh_user as u','l.proposer_id=u.id')
                        ->leftJoin('gky_qyh_user as ul','l.approver_id=ul.id')
                        ->where(['print_code' => $printCode])
                        ->one();
    }
    public static function agreeStatement($printCode,$opinion){
        $model= GkyQyhSpStatement::findOne(['print_code'=>$printCode]);
        $model->status=1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
    public static function refuseStatement($printCode,$opinion){
        $model= GkyQyhSpStatement::findOne(['print_code'=>$printCode]);
        $model->status=-1;
        $model->opinion=$opinion;
        $model->agree_time=date('Y-m-d H:m:s');
        return $model->save();
    }
}
